package luccy.behavior;

import luccy.attribute.AttributeContainer;
import luccy.container.ContainerAware;
import luccy.container.ExternalAware;
import luccy.container.Registry;

public class RequestBehavior implements RuntimeBehavior, ExternalAware, ContainerAware {

	private Registry container;

	private AttributeContainer request;

	public RequestBehavior() {
	}

	@SuppressWarnings("unchecked")
	public <T> T get(Class<? super T> clazz) {
		Object attribute = request.getAttribute(clazz.getName());
		if (attribute == null) {
			T t = container.getComponent(clazz);
			request.setAttribute(clazz.getName(), t);
			return t;
		} else {
			return (T) attribute;
		}
	}

	public void setAttributeContainer(AttributeContainer request) {
		this.request = request;
	}

	public void setCointainer(Registry container) {
		this.container = container;
	}

}
